<?php

namespace BI\Controller;
use Common\Controller;
/**
 * Description of IndexController
 *
 * @author v
 */
class AnalysisController extends CommonController{
    public function index(){
        $this->display();
    }
    
    public function cross(){
        $code = 'Spaceplus';
        $this->assign('code',$code);
        
        $AnalyzeData=new BdataConfigController($code,1);
		
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	    $this->assign('Condition',$getCondition);

        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        
        
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
        $this->assign('count',M()->query('select id,code,title from mt_count'));
        
        $this->display();
    }
	
    public function customer(){
        $code = 'Customer';
        $this->assign('code',$code);
        
        $AnalyzeData=new BdataConfigController($code,1);
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	    $this->assign('Condition',$getCondition);

        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
         $this->assign('count',M()->query('select id,code,title from mt_count'));
        
        $this->display();
    }
	
    public function supply(){
        $code = 'Supply';
        $this->assign('code',$code);
        
        $AnalyzeData=new \Common\Controller\BdataConfigController($code,1);
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	$this->assign('Condition',$getCondition); 
        
        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
         $this->assign('count',M()->query('select id,code,title from mt_count'));
        
        $this->display();
    }
    /*
     * 保存金蛋
     */
    public function saveEgg(){
        $model=I('post.');
        $uid = $_SESSION['userInfo']['id'];

        $data=array(
            'uid'=>$uid,
            'status'=>1,
            'createTime'=>time(),
            'lastid'    =>0,
            'isSystem' => $model['ui']['isSystem']
        );
        unset($model['ui']['isSystem']);
        if(!empty($model['id'])){
            $data['lastid']=$model['id'];
        }

        $data['quickCondition']=  serialize($model['dwhere']);
        unset($model['dwhere']);  //删除条件数组元素
        $data['ui']=serialize($model['ui']);
        unset($model['ui']);//UI项
        $where=$this->dealArrayEmpty($model['where']);

        $data['where']=array(
            array(
                'uid'=>$uid,
                'createTime'=>time(),
                'isdefault'=>1,
                'title'=>'默认条件',
                'defaultConditions'=>serialize($where)
            )
        );
        if(!empty($model['conditionGroup'])){
            foreach($model['conditionGroup'] as $val){
                $title=$val['title'];
                unset($val['title']);
                $dCond=$this->dealArrayEmpty($val);
                $data['where'][]=array(
                    'uid'=>$uid,
                    'createTime'=>time(),
                    'isdefault'=>0,
                    'title'=>$title,
                    'defaultConditions'=>serialize($dCond)
                );
            }
        }
       
        unset($model['conditionGroup']);
        unset($model['where']);  //保存默认条件
        $data['modelData']=serialize($model);unset($model);

        $obj=D('Eggs');
        /*
        if($obj->relation(true)->add($data)){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
        
         */
    }
    
    public function AnalyzeData(){
        
        $param=I('post.');       

        set_time_limit(0);
        //$AnalyzeData=new \Common\Controller\BdataResultController($param);
        $AnalyzeData=new ReturnResultController($param);
        $data=$AnalyzeData->getAnalyzeDataResults();
        //$this->behavior($param);

        //若不需要链接，则清空id
        /*
        if($this->LinkMode == 0){
            foreach($data['d']['r']['i'] as $key=>$val){
                unset($data['d']['r']['i'][$key]['id']);
            }
        }        
        */
        $this->ajaxReturn($data);
    }
    
    public function ViewData(){
        $param=I('post.');     
        //$param['code'] = 'Bdata';
        $AnalyzeData=new ViewDataController('mt_AnalyTable', $param);
        //$data=$AnalyzeData->getAnalyzeDataResults();
        $data=$AnalyzeData->getViewDataResults();
                //var_dump($data);
        $this->ajaxReturn($data);
    }
    
    public function rank(){
        $params = I('get.');
        $code = empty($params['code']) ? 'Spaceplus' : $params['code'];
        $this->assign('code',$code);
        
        $AnalyzeData=new \Common\Controller\BdataConfigController($code,1);
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	$this->assign('Condition',$getCondition); 

        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
        $this->assign('count',M()->query('select id,code,title from mt_count'));
        
        $this->display();
    }
    
    /**
     * 存量分析
     */
    public function stock(){
        $code = 'Stock';
        $this->assign('code',$code);
        
        $AnalyzeData=new \Common\Controller\BdataConfigController($code,1);
        //$getWhere=$AnalyzeData->fetchAllConditions();
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	$this->assign('Condition',$getCondition); 

        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
        $this->assign('count',M()->query('select id,code,title from mt_count'));
         
        $this->display();
    }
    
    
    public function StockAnalyzeData(){
        $param=I('post.');       
        set_time_limit(0);
        //$AnalyzeData=new \Common\Controller\BdataResultController($param);
        $AnalyzeData=new \Common\Controller\ReturnResultController($param);
        $data=$AnalyzeData->getAnalyzeDataResults();
        $this->behavior($param);
        
        //若不需要链接，则清空id
        /*
        if($this->LinkMode == 0){
            foreach($data['d']['r']['i'] as $key=>$val){
                unset($data['d']['r']['i'][$key]['id']);
            }
        }  
        */
        $this->ajaxReturn($data);
    }
    
    public function StockViewData(){
        $param=I('post.');     
        //var_dump($param);
        $AnalyzeData=new \Common\Controller\ViewDataController('mt_AnalyTable', $param);
        //$data=$AnalyzeData->getAnalyzeDataResults();
        $data=$AnalyzeData->getViewDataResults();
                //var_dump($data);
        $this->ajaxReturn($data);
    }
}